#ifndef _COMM_HPP
#define _COMM_HPP_

#include"log.hpp"
#include<iostream>
#include<string>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<stdio.h>

using namespace std;

const int size=4096;
const string pathname="/home/zwt";
const int proj_id=0x6666;
Log log;

key_t GetKey()
{
    key_t k=ftok(pathname.c_str(),proj_id);
    if(k==-1)
    {
        log(Fatal,"ftok error: %s",strerror(errno));
        exit(1);
    }
    log(Info,"ftok success,key is: %d",k);
    return k;
}

int GetShareMemHelp(int flag)
{
    key_t k=GetKey();
    int shmid=shmget(k,size,IPC_CREAT|IPC_EXCL|0666);
    if(shmid<0) //创建失败
    {
        log(Fatal,"create share memory error:%s",strerror(errno));
        exit(2);
    }
    log(Info,"create share memory success,shmid: %d",shmid);

    return shmid;
}

int CreateShm()  //创建共享内存
{
    return GetShareMemHelp(IPC_CREAT|IPC_EXCL|0666);
}

int GetShm()   //获取共享内存
{
    return GetShareMemHelp(IPC_CREAT);
}
#endif